package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.pojo.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:JinFeng
 * @Date: 2025/06/13 20:49
 * @Description:
 */
@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "后台菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> Update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}", dishDTO);
        return dishService.updateWithFlavors(dishDTO);
    }





    @ApiOperation("根据ID查询菜品及口味数据")
    @GetMapping("/{id}")
    public Result<DishVO>  findById(@PathVariable(name = "id") Long id){
        log.info("根据ID查询菜品及口味数据,id:{}", id);
        return dishService.findByIdWithFlavors(id);
    }



    @ApiOperation("根据Id列表删除")
    @DeleteMapping
    public Result<String> deleteByIds(@RequestParam(name  = "ids") List<Long> ids){
        log.info("删除菜品:{}", ids);
        return dishService.deleteByIds(ids);
    }




    @ApiOperation("菜品分页条件查询")
    @GetMapping("/page")
    public Result<PageResult> pageQury( DishPageQueryDTO dishPageQueryDTO){
        log.info("分页条件查询:{}",  dishPageQueryDTO);
        return dishService.pageQuery(dishPageQueryDTO);

    }


    @ApiOperation("添加菜品及口味")
    @PostMapping
    public Result<String> add(@RequestBody DishDTO dishDTO){
        return dishService.addWithFlavor(dishDTO);
    }

   /* @ApiOperation("根据菜品名称name查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list( String name){
        return dishService.findByCategoryByName(name);
    }*/
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(@RequestParam(name = "categoryId")Long categoryId){
        return dishService.findByCategoryId(categoryId);
    }*/
    @ApiOperation("根据菜品名称name查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(@RequestParam(required = false) String name, @RequestParam(required = false) Long categoryId) {
        if (name != null) {
            return dishService.findByCategoryByName(name);
        } else if (categoryId != null) {
            return dishService.findByCategoryId(categoryId);
        }
        return Result.success(new ArrayList<>()); // 默认返回空列表
    }

}
